﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace Groep2TINGHService.DB_Classes
{
    public class ContractFormulaDB
    {
        //Geeft de contractomschrijving terug
        public static List<ContractFormula> GetAllContractFormula()
        {
            List<ContractFormula> lijst = new List<ContractFormula>();
            SqlConnection connection = ConnectionDB.GetConnection();
            SqlCommand selectcommand = new SqlCommand("SELECT Description FROM ContractFormula", connection);
            try
            {
                ContractFormula temp;
                connection.Open();
                SqlDataReader reader = selectcommand.ExecuteReader();

                int Description = reader.GetOrdinal("Description");

                while (reader.Read())
                {
                    temp = new ContractFormula();
                    temp.Description = reader.GetString(Description);
                    lijst.Add(temp);
                }
                reader.Close();
                return lijst;
            }
            catch (SqlException ex)
            {
                throw;
            }
            finally
            {
                connection.Close();
            }
        }
        public static ContractFormula GetContractDurationByDescription(ContractFormula Description)
        {
            ContractFormula temp = new ContractFormula();
            String sql = "SELECT PeriodInMonths FROM ContractFormula WHERE Description = @Description";
            SqlConnection connection = ConnectionDB.GetConnection();
            SqlCommand selectCommand = new SqlCommand(sql, connection);
            selectCommand.Parameters.AddWithValue("@Description", Description.Description);

            try
            {
                connection.Open();
                SqlDataReader reader = selectCommand.ExecuteReader();
                if (reader.Read())
                {
                    int PeriodsInMonths = reader.GetOrdinal("PeriodInMonths");

                    temp.PeriodInMonths = reader.GetInt32(PeriodsInMonths);
                }
                reader.Close();
                return temp;
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                connection.Close();
            }
        }
    }
}